{{- define "api_proxy_resources" }}
cpu: 10m
memory: 25Mi
{{- end }}

{{- if .Values.istio.multicluster.enabled }}
  {{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: api-proxy
  namespace: d8-{{ $.Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "api-proxy" "workload-resource-policy.deckhouse.io" "every-node")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: api-proxy
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: api-proxy
      minAllowed:
        {{- include "api_proxy_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 20m
        memory: 50Mi
  {{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-proxy
  namespace: d8-{{ $.Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "api-proxy")) | nindent 2 }}
spec:
  {{- include "helm_lib_deployment_strategy_and_replicas_for_ha" . | nindent 2 }}
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: api-proxy
  template:
    metadata:
      labels:
        app: api-proxy
    spec:
      {{- include "helm_lib_node_selector" (tuple . "master") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple . "system-cluster-critical") | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "api-proxy")) | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" . | nindent 6 }}
      serviceAccountName: multicluster-api-proxy
      imagePullSecrets:
      - name: deckhouse-registry
      containers:
      - name: api-proxy
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" . | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "apiProxy") }}
        imagePullPolicy: IfNotPresent
        env:
        - name: CLUSTER_DOMAIN
          value: {{ .Values.global.discovery.clusterDomain }}
        - name: CLUSTER_UUID
          value: {{ .Values.global.discovery.clusterUUID }}
        ports:
        - name: https
          containerPort: 4443
        readinessProbe:
          httpGet:
            path: /ready
            port: https
            scheme: HTTPS
          initialDelaySeconds: 1
          periodSeconds: 30
        livenessProbe:
          httpGet:
            path: /healthz
            port: https
            scheme: HTTPS
          initialDelaySeconds: 1
          periodSeconds: 30
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
  {{- if not (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "api_proxy_resources" . | nindent 12 }}
  {{- end }}
        volumeMounts:
        - name: remote-public-metadata
          mountPath: /remote/
        - name: kube-rbac-proxy-ca
          mountPath: /etc/ssl/kube-rbac-proxy-ca.crt
          subPath: ca.crt
      volumes:
      - name: remote-public-metadata
        secret:
          defaultMode: 420
          secretName: d8-remote-clusters-public-metadata
      - name: kube-rbac-proxy-ca
        configMap:
          defaultMode: 420
          name: kube-rbac-proxy-ca.crt
{{- end }}
